package com.example.taobaolianmeng.presenter.impl;

import android.util.Log;

import com.example.taobaolianmeng.model.Api;
import com.example.taobaolianmeng.model.domain.OnSellContent;
import com.example.taobaolianmeng.presenter.IOnSellPagePresenter;
import com.example.taobaolianmeng.utils.RetrofitManager;
import com.example.taobaolianmeng.utils.UrlUtils;
import com.example.taobaolianmeng.view.IOnSellPageCallback;

import java.net.HttpURLConnection;

import retrofit2.Call;
import retrofit2.Callback;
import retrofit2.Response;
import retrofit2.Retrofit;

public class OnSellPagePresenterImpl implements IOnSellPagePresenter {

    public static final int DEFAULT_PAGE =1;
    private static final String TAG = "OnSellPagePresenterImpl" ;
    private int mCurrentPage = DEFAULT_PAGE;
    private IOnSellPageCallback mOnSellPageCallback= null;
    private final Api mApi;


    public OnSellPagePresenterImpl(){
        Retrofit retrofit = RetrofitManager.getInstance().getRetrofit();
        mApi = retrofit.create(Api.class);
    }

    @Override
    public void getOnSellContent() {
        if(mIsLoading){
            return;
        }
        mIsLoading =true;
        //通知ui状态为加载中。。
        if (mOnSellPageCallback!=null) {
            mOnSellPageCallback.onLoading();
        }

        //获取特惠内容

        String targetUrl = UrlUtils.getOnSellPageUrl(mCurrentPage);
        Call<OnSellContent> task = mApi.getOnSellPageContent(targetUrl);
        task.enqueue(new Callback<OnSellContent>() {
            @Override
            public void onResponse(Call<OnSellContent> call, Response<OnSellContent> response) {
                mIsLoading =false;
                int code = response.code();
                if(code == HttpURLConnection.HTTP_OK){
                    OnSellContent result = response.body();
                    onSuccess(result);
                }else{
                    onError();
                }
            }

            @Override
            public void onFailure(Call<OnSellContent> call, Throwable t) {
                onError();
            }
        });
    }

    private void onSuccess(OnSellContent result) {
        if (mOnSellPageCallback != null) {
            try{
                int size = result.getData().getTbk_dg_optimus_material_response().getResult_list().getMap_data().size();
                if(size==0){
                    onEmpty();
                }else {
                    mOnSellPageCallback.onContentLoadedSuccess(result);
                }
            }catch (Exception e){
                e.printStackTrace();
                onEmpty();
            }
        }
    }


    private void onEmpty(){
        if(mOnSellPageCallback !=null){
            mOnSellPageCallback.onEmpty();
        }
    }

    private void onError() {
        mIsLoading =false;
        if (mOnSellPageCallback != null) {
            mOnSellPageCallback.onNetworkError();
        }
    }

    @Override
    public void reLoad() {
        //重新加载
        this.getOnSellContent();
    }

    //当前状态
    private  boolean mIsLoading =false;

    @Override
    public void loaderMore() {
        if(mIsLoading){
            return;
        }
        mIsLoading =true;
        //加载更多
        mCurrentPage++;
        //加载更多
        String targetUrl = UrlUtils.getOnSellPageUrl(mCurrentPage);
        Call<OnSellContent> task = mApi.getOnSellPageContent(targetUrl);
        task.enqueue(new Callback<OnSellContent>() {
            @Override
            public void onResponse(Call<OnSellContent> call, Response<OnSellContent> response) {
                mIsLoading =false;
                int code = response.code();
                Log.d(TAG,"code is "+code);
                if(code == HttpURLConnection.HTTP_OK){
                    OnSellContent result = response.body();
                    onMoreLoaded(result);
                }else{
                    onMoreLoadError();
                }
            }

            @Override
            public void onFailure(Call<OnSellContent> call, Throwable t) {
                onMoreLoadError();
            }
        });
    }

    private void onMoreLoadError() {
        mIsLoading =false;
        mCurrentPage--;
        mOnSellPageCallback.onMoreLoadedError();
    }

    private void onMoreLoaded(OnSellContent result) {
        if (mOnSellPageCallback != null) {
            if(isEmpty(result)){
                mCurrentPage--;
                mOnSellPageCallback.onMoreLoadedEmpty();
            }else {
                mOnSellPageCallback.onMoreLoaded(result);
            }
        }
    }

    private boolean isEmpty(OnSellContent content) {
        int size = content.getData().getTbk_dg_optimus_material_response().getResult_list().getMap_data().size();
        return size == 0;
    }

    @Override
    public void registerViewCallback(IOnSellPageCallback callBack) {
        this.mOnSellPageCallback = callBack;
    }

    @Override
    public void unregisterViewCallback(IOnSellPageCallback callBack) {
        this.mOnSellPageCallback = null;
    }
}
